package com.ysxsoft.wdg.ui.activity.center_zy;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.PingLunAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.BaseBean;
import com.ysxsoft.wdg.bean.DiscussBean;
import com.ysxsoft.wdg.bean.DiscussBean2;
import com.ysxsoft.wdg.constant.MCConfig;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.ViewUtils;
import com.ysxsoft.wdg.view.EStarView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bingoogolapple.photopicker.activity.BGAPhotoPreviewActivity;
import cn.bingoogolapple.photopicker.widget.BGANinePhotoLayout;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class GoodsPlActivity extends BaseActivity implements OnLoadmoreListener, OnRefreshListener {

    public static void start(Context context, String productid) {
        Intent intent = new Intent(context, GoodsPlActivity.class);
        intent.putExtra("productid", productid);
        context.startActivity(intent);
    }

    public static void start(Context context, String productid, String type) {
        Intent intent = new Intent(context, GoodsPlActivity.class);
        intent.putExtra("productid", productid);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.title_iv_r)
    ImageView titleIvR;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;

    private PingLunAdapter adapter;
    private String productid = "";
    private int page = 1;
    private String type = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_list;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, false);
        titleContent.setText("全部评论");
        productid = getIntent().getStringExtra("productid");
        type = getIntent().getStringExtra("type");
        recyclerview.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        adapter = new PingLunAdapter(R.layout.item_pl);
        recyclerview.setAdapter(adapter);
        multStatusView.showLoading();
        onRefresh(refreshLayout);
        getList();
    }

    @Override
    protected void setListener() {
        refreshLayout.setOnLoadmoreListener(this);
        refreshLayout.setOnRefreshListener(this);
    }


    @OnClick(R.id.title_finish)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        refreshlayout.finishLoadmore(600);

        page++;
        getList();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        refreshlayout.finishRefresh(600);

        page = 1;
        adapter.clear();
        getList();
    }

    private void getList() {
        if(type!=null){//商品评价列表
            mApiHelper.getZyDiscussList2(productid, page + "",type, new Observer<BaseBean<List<DiscussBean2>>>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(BaseBean<List<DiscussBean2>> baseBean) {
                    multStatusView.showContent();
                    if (baseBean.getCode().equals(CallbackCode.SUCCESS)) {
                        if (page == 1) {
                            adapter.setNewData(baseBean.getData());
                        } else {
                            adapter.addData(baseBean.getData());
                        }
                    } else {
                        ToastUtils.showToast(baseBean.getMs());
                    }
                }

                @Override
                public void onError(Throwable e) {
                    multStatusView.showError();
                }

                @Override
                public void onComplete() {

                }
            });
        }else{//自营商品评价列表
            mApiHelper.getZyDiscussList(productid, page + "", new Observer<BaseBean<List<DiscussBean2>>>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(BaseBean<List<DiscussBean2>> baseBean) {
                    multStatusView.showContent();
                    if (baseBean.getCode().equals(CallbackCode.SUCCESS)) {
                        if (page == 1) {
                            adapter.setNewData(baseBean.getData());
                        } else {
                            adapter.addData(baseBean.getData());
                        }
                    } else {
                        ToastUtils.showToast(baseBean.getMs());
                    }
                }

                @Override
                public void onError(Throwable e) {
                    multStatusView.showError();
                }

                @Override
                public void onComplete() {

                }
            });
        }
    }

    class PingLunAdapter extends BaseQuickAdapter<DiscussBean2, BaseViewHolder> {


        public PingLunAdapter(int layoutResId) {
            super(layoutResId);
        }

        public void clear() {
            getData().clear();
            notifyDataSetChanged();
        }

        @Override
        protected void convert(BaseViewHolder helper, DiscussBean2 item) {
            helper.setText(R.id.tv_pl_nick, item.getNickname());
            helper.setText(R.id.tv_pl_content, item.getContent());
            helper.setText(R.id.tv_time, item.getAddtime());
            EStarView view = helper.getView(R.id.starView);
            LinearLayout starLayout = helper.getView(R.id.starLayout);
            starLayout.setVisibility(View.GONE);

            ViewUtils.loadCircleImage(mContext, item.getAvatar(), (ImageView) helper.getView(R.id.iv_pl_tx));


            BGANinePhotoLayout bgaNinePhotoLayout = helper.getView(R.id.photos);
            bgaNinePhotoLayout.setData((ArrayList<String>) item.getPic());
            bgaNinePhotoLayout.setDelegate(new BGANinePhotoLayout.Delegate() {
                @Override
                public void onClickNinePhotoItem(BGANinePhotoLayout ninePhotoLayout, View view, int position, String model, List<String> models) {
                    File downloadDir = new File(MCConfig.PICTURE_DIR);
                    BGAPhotoPreviewActivity.IntentBuilder photoPreviewIntentBuilder = new BGAPhotoPreviewActivity.IntentBuilder(mContext)
                            .saveImgDir(downloadDir); // 保存图片的目录，如果传 null，则没有保存图片功能
                    photoPreviewIntentBuilder
                            .previewPhotos((ArrayList<String>) models).currentPosition(position); // 当前预览图片的索引
                    mContext.startActivity(photoPreviewIntentBuilder.build());
                }
            });

        }
    }
}
